#include "atomic_0828_object_detection_and_classification_service.h"

int getObjectDetectClassify(DetectClassifyInput *input, DetectClassifyOutput *output)
{
    // 入参合法性检查
    if (input == nullptr || output == nullptr)
    {
        return ATOMIC_SERVICE_ERROR;
    }

    // 初始化输出结构体
    memset(output, 0, sizeof(DetectClassifyOutput));

    // 验证输入图像尺寸有效性
    if (input->img_width <= 0 || input->img_height <= 0)
    {
        output->return_type = ATOMIC_SERVICE_ERROR;
        return ATOMIC_SERVICE_ERROR;
    }

    // 验证特征参数范围有效性
    if (input->edge_strength < 0 || input->edge_strength > 100 ||
        input->texture_score < 0 || input->texture_score > 100)
    {
        output->return_type = ATOMIC_SERVICE_ERROR;
        return ATOMIC_SERVICE_ERROR;
    }

    // 基于输入特征模拟检测逻辑
    // 1. 确定目标数量(根据特征数量判断)
    output->object_count = (input->feature_count > 0) ? 1 : 0;

    if (output->object_count > 0)
    {
        // 2. 确定目标类别(基于边缘强度判断)
        if (input->edge_strength >= 70)
        {
            output->class_id = 1; // 人
        }
        else if (input->edge_strength >= 40)
        {
            output->class_id = 2; // 车
        }
        else if (input->edge_strength >= 20)
        {
            output->class_id = 3; // 动物
        }
        else
        {
            output->class_id = 4; // 其他
        }

        // 3. 计算置信度(基于纹理评分)
        output->confidence = input->texture_score;

        // 4. 计算目标坐标(基于图像尺寸的10%-90%范围)
        output->left_x = static_cast<int>(input->img_width * 0.1);
        output->left_y = static_cast<int>(input->img_height * 0.1);
        output->right_x = static_cast<int>(input->img_width * 0.9);
        output->right_y = static_cast<int>(input->img_height * 0.9);

        // 确保坐标有效性
        output->left_x = (output->left_x < 0) ? 0 : output->left_x;
        output->left_y = (output->left_y < 0) ? 0 : output->left_y;
        output->right_x = (output->right_x >= input->img_width) ? (input->img_width - 1) : output->right_x;
        output->right_y = (output->right_y >= input->img_height) ? (input->img_height - 1) : output->right_y;

        // 5. 设置目标ID(示例固定为1)
        output->target_id = 1;

        // 6. 获取当前时间戳(秒)
        auto now = std::chrono::system_clock::now();
        output->result_time = static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(
            now.time_since_epoch()
        ).count());

        // 7. 确定是否需要推送(置信度>50时推送)
        output->need_push = (output->confidence > 50) ? 1 : 0;
    }

    // 设置成功状态码
    output->return_type = ATOMIC_SERVICE_SUCCESS;
    return ATOMIC_SERVICE_SUCCESS;
}